<template>
  <el-dialog
    :close-on-click-modal="false"
    class="assign-role"
    title="分配角色"
    :visible="showRoleDialog"
    @close="close"
  >
    <!-- 这里准备复选框 -->
    <el-checkbox-group v-model="roleIds">
      <!-- 注意：label决定当前选中的值 -->
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <template #footer>
      <el-button type="primary" size="small" @click="assignRole">
        确定
      </el-button>
      <el-button size="small" @click="close()">取消</el-button>
    </template>
  </el-dialog>
</template>

<script>
// 获取角色列表
import { getRoleList } from '@/api/setting'
// 获取当前员工的角色列表
import { getUserDetailById } from '@/api/user'
// 点击确定给员工分配角色
import { assignRoles } from '@/api/employees'
export default {
  name: 'AssignRole',
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      // 所有角色列表
      list: [],
      // 选中的角色ID 列表
      roleIds: [],
      // { id:当前用户id, roleIds:选中的角色id组成的数组 }
      userId: ''

    }
  },
  created () {
    this.getRoleLists()
  },
  methods: {
    // 点击确定给员工分配角色
    async assignRole () {
      try {
        await assignRoles({ id: this.userId, roleIds: this.roleIds })
        this.$message.success('分配成功')
        this.close()
      } catch (error) {
        console.log(error)
      }
    },
    close () {
      this.$emit('update:showRoleDialog', false)
    },
    async getRoleLists () {
      const data = await getRoleList({ page: 1, pagesize: 1000 })
      this.list = data.rows
      console.log(data.rows)
    },
    // 获取当前员工的角色列表
    async getRoleDetailApi (id) {
      const detailList = await getUserDetailById(id)
      console.log(detailList)
      this.roleIds = detailList.roleIds
      this.userId = id
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-checkbox {
  margin-bottom: 20px;
}
</style>
